/*!	 widget_time.cpp
**	 Template File
**
**	Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
**	Copyright (c) 2008 Chris Moore
**  Copyright (c) 2008 Paul Wise
**
**	This package is free software; you can redistribute it and/or
**	modify it under the terms of the GNU General Public License as
**	published by the Free Software Foundation; either version 2 of
**	the License, or (at your option) any later version.
**
**	This package is distributed in the hope that it will be useful,
**	but WITHOUT ANY WARRANTY; without even the implied warranty of
**	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
**	General Public License for more details.
**
*/

#ifdef USING_PCH
#	include "pch.h"
#else
#ifdef HAVE_CONFIG_H
#	include <config.h>
#endif

#include <synfig/general.h>

#include <gtkmm/entry.h>
#include <gtkmm/button.h>
#include "widgets/widget_time.h"
#include "app.h"

#include <gui/localization.h>

#endif

using namespace std;
// using namespace etl;
using namespace synfig;
using namespace studio;

Widget_Time::Widget_Time():
    fps_(0),
    time_(0)
{
    signal_activate().connect(sigc::mem_fun(*this, &studio::Widget_Time::refresh_value));
    signal_activate().connect(sigc::mem_fun(*this, &studio::Widget_Time::refresh_text));

    add_events(Gdk::SCROLL_MASK);
}

Widget_Time::~Widget_Time()
{
}

void
Widget_Time::refresh_text()
{
    set_text(time_.get_string(fps_, App::get_time_format()));
}

void
Widget_Time::set_value(const synfig::Time &data)
{
    time_ = data;
    refresh_text();
}

synfig::Time
Widget_Time::get_value() const
{
    return time_;
}

void
Widget_Time::set_fps(float x)
{
    fps_ = Time(x);
    refresh_text();
}

void
Widget_Time::refresh_value()
{
    try {
        Time newtime(get_text(), fps_);

        if (abs(newtime - time_) >= 0.001) {
            time_ = newtime;
            refresh_text();
            signal_value_changed()();
        }
    } catch (...) {
        throw string("Caught unknown exception");
    }
}

bool
Widget_Time::on_event(GdkEvent* event)
{
    const Time scroll_amount(0.25);

    switch (event->type) {
    case GDK_SCROLL:
        if (event->scroll.direction == GDK_SCROLL_DOWN || event->scroll.direction == GDK_SCROLL_LEFT) {
            time_ -= scroll_amount;
            refresh_text();
            signal_value_changed()();
        } else if (event->scroll.direction == GDK_SCROLL_UP || event->scroll.direction == GDK_SCROLL_RIGHT) {
            time_ += scroll_amount;
            refresh_text();
            signal_value_changed()();
        }

        return true;
        break;

    case GDK_BUTTON_PRESS:
    case GDK_2BUTTON_PRESS:
    case GDK_3BUTTON_PRESS:
        if (!has_focus()) {
            grab_focus();
        }

        break;

    default:
        break;
    }

    return Gtk::Entry::on_event(event);
}

bool
Widget_Time::on_focus_out_event(GdkEventFocus* event)
{
    refresh_value();
    refresh_text();
    return Gtk::Entry::on_focus_out_event(event);
}

bool
Widget_Time::on_focus_in_event(GdkEventFocus* event)
{
    // if defined, show the full time format "0h 0m 5s 0f" when the time widget gets focus
    if (getenv("SYNFIG_SHOW_FULL_TIME_ON_FOCUS")) {
        set_text(time_.get_string(fps_, App::get_time_format() | Time::FORMAT_FULL));
    }

    return Gtk::Entry::on_focus_in_event(event);
}